#pragma once
#include<iostream>
#include<vector>
#include<string>
#include<mutex>
#include<assert.h>
#include<fstream>
// #include"./oj_model.hpp"
#include"./oj_model2.hpp"

#include"../common/log.hpp"
#include"../common/util.hpp"
#include<ctemplate/template.h>

namespace ns_view
{
    using namespace std;
    using namespace ns_model;
    using namespace ns_log;
    using namespace ns_util;
    using namespace httplib;

    class View
    {
    public:
        View()
        {}
        ~View()
        {}
    public:
    // struct Question
    // {
    //     std::string number; // 题目编号，唯一
    //     std::string title;  // 题目的标题
    //     std::string star;   // 难度: 简单 中等 困难
    //     int cpu_limit;      // 题目的时间要求(S)
    //     int mem_limit;      // 题目的空间要去(KB)
    //     std::string desc;   // 题目的描述
    //     std::string header; // 题目预设给用户在线编辑器的代码
    //     std::string tail;   // 题目的测试用例，需要和header拼接，形成完整代码
    // };
        const std::string template_path = "./template_html/";
        bool AllExpandHtml(const vector<Question> &allq,std::string *html)
        {
            //题目编号 题目标题 题目难度
            //1.获取题目路径
            std::string src = template_path + "allquestion.html";

            //2.形成数据字典
            ctemplate::TemplateDictionary root("allquestion");
            for(const auto &q : allq)
            {
                ctemplate::TemplateDictionary *sub = root.AddSectionDictionary("questions_list");
                sub->SetValue("number",q.number);
                sub->SetValue("title",q.title);
                sub->SetValue("star",q.star);
            }
            //3.获取被渲染的html
            ctemplate::Template *tpl = ctemplate::Template::GetTemplate(src,ctemplate::DO_NOT_STRIP);
            //4.将渲染的html返回给上层  
            tpl->Expand(html,&root);

            return true;
        }
        bool OneExpandHtml(const Question &q,std::string *html)
        {
            //1.获取题目路径
            std::string src = template_path + "onequestion.html";

            //2.形成数据字典
            ctemplate::TemplateDictionary root("onequestion");

            root.SetValue("number",q.number);
            root.SetValue("title",q.title);
            root.SetValue("star",q.star);
            root.SetValue("desc",q.desc);
            root.SetValue("pre_code",q.header);

            //3.获取被渲染的html
            ctemplate::Template *tpl = ctemplate::Template::GetTemplate(src,ctemplate::DO_NOT_STRIP);
            //4.将渲染的html返回给上层  
            tpl->Expand(html,&root);
            return true;
        }

    };
}
